package src.consumer_producer;

import java.util.Random;

/**
 * Created by 58 on 2016/3/23.
 */
public class Producer extends Thread {
    private final Random random;
    private Table table;
    private static int id = 0;

    public Producer(String name, Table table, long seed) {
        super(name);
        this.table = table;
        this.random = new Random(seed);
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(random.nextInt(1000));
                String cake = "[ cake num" + nextId() + " by " + getName() + "]";
                table.put(cake);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static int nextId() {
        return id++;
    }
}
